from app.services.ocr.utils.text_processing import extract_question_rich, extract_ocr_options,extract_question
from app.services.ocr.utils.utils import is_tf


def prepare_query_text(raw_text):
    """
    从OCR文本中提取题干和选项，
    对选项排序后拼接返回
    """
    # 提取题干
    subject = extract_question_rich(raw_text)
    if is_tf(raw_text):
        return subject

    # 提取选项
    _, ocr_options = extract_ocr_options(raw_text)
    options = [opt[1].strip() for opt in ocr_options if opt[1].strip()]
    options.sort()

    if not options:
        return extract_question(raw_text)

    # 拼接题干 + 排序后的选项
    full_text = subject + ' ' + ' '.join(options)
    return full_text
